/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farmcode.base.web;

import java.util.TimeZone;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

/**
 *
 * @author oiprado
 */
public abstract class BaseBacking {

    protected static final String GLOBAL_LOOK_UP = "_look_up_name";
    protected static final String GLOBAL_LOOK_UP_CALLBACK = "_look_up_callback";
    private boolean authenticated;
    private LookUpCallBack lookUpCallBack;
    protected Session session;
    protected Application application;
    private TimeZone timeZone;
    private Identity identity;

    public BaseBacking() {
        session = new Session();
        application = new Application();
        timeZone = java.util.TimeZone.getDefault();
    }

    protected void showMessage(String message){
        FacesContext facesContext = FacesContext.getCurrentInstance();
        facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, message, message));
    }
    
    protected void showWarning(String message){
        FacesContext facesContext = FacesContext.getCurrentInstance();
        facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, message, message));
    }
    
    protected void showError(String message){
        FacesContext facesContext = FacesContext.getCurrentInstance();
        facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, message, message));
    }
    
    /**
     * @return the session
     */
    public Session getSession() {
        return session;
    }

    /**
     * @param session the session to set
     */
    public void setSession(Session session) {
        this.session = session;
    }

    /**
     * @return the timeZone
     */
    public TimeZone getTimeZone() {
        return timeZone;
    }

    /**
     * @param timeZone the timeZone to set
     */
    public void setTimeZone(TimeZone timeZone) {
        this.timeZone = timeZone;
    }

//  public abstract void beforeCreate();
//  public abstract void afterCreate();
//  
    /**
     * @return the lookUpCallBack
     */
    public LookUpCallBack<?> getLookUpCallBack() {
        if (getSession().getValue(GLOBAL_LOOK_UP_CALLBACK) != null) {
            lookUpCallBack = getSession().getValue(GLOBAL_LOOK_UP_CALLBACK);
        }
        return lookUpCallBack;
    }

    /**
     * @param lookUpCallBack the lookUpCallBack to set
     */
    public void setLookUpCallBack(LookUpCallBack<?> lookUpCallBack) {
        this.lookUpCallBack = lookUpCallBack;
        getSession().setValue(GLOBAL_LOOK_UP_CALLBACK, lookUpCallBack);
    }

    protected void openLookUp(Class name) {
        getSession().setValue(GLOBAL_LOOK_UP, name.getSimpleName());
    }

    protected void openLookUp(Class name, LookUpCallBack<?> origen) {
        openLookUp(name);
        setLookUpCallBack(origen);
    }

    /**
     * @return the application
     */
    public Application getApplication() {
        return application;
    }

    /**
     * @param application the application to set
     */
    public void setApplication(Application application) {
        this.application = application;
    }

    /**
     * @return the authenticated
     */
    public boolean isAuthenticated() {
        return authenticated;
    }

    /**
     * @param authenticated the authenticated to set
     */
    public void setAuthenticated(boolean authenticated) {
        this.authenticated = authenticated;
    }

    /**
     * @return the identity
     */
    public Identity getIdentity() {
        if (getSession().getValue("identity") != null) {
            identity = getSession().getValue("identity");
        } else {
            setIdentity(new Identity());
        }
        return identity;
    }

    /**
     * @param identity the identity to set
     */
    public void setIdentity(Identity identity) {
        this.identity = identity;
        getSession().setValue("identity", identity);
    }
}
